################################### Packages ##########################################
library(tidyverse)
library(fixest)
library(did)
library(dotwhisker)
library(broom)

################################### Set Working Directory ##########################################

# Getting the path of your current file
setwd(dirname(rstudioapi::getSourceEditorContext()$path))

################################### functions ##########################################
# IHS Transformation
ihs=function(x) log(x+sqrt(1+x^2)) # to revers ihs use sinh
# Demeans and IHS Transforms
idm=function(x) ihs(x)-mean(ihs(x),na.rm=T)
# Demeans
dm=function(x) x-mean(x,na.rm=T)


#################################### Read data ###############################
### To create the "Table A.12: Large fishing vessels, property rights, and credit market development" table replace data by "data_boats_large_final"
### Reads boats data and transforms than similar to the main regression
x = read.csv("../Data/data_boats_final.csv",stringsAsFactors = F)%>%
  filter(year%in%1960:2021 & gamma !="all")%>%
  group_by(country)%>% 
  mutate(gdp=idm(gdp_const),
         gdp2=idm(gdp_const)^2,
         tfp=idm(rtfpna),
         fao_ag_credit = idm(fao_agriculture_credit),
         fao_to_credit = idm(fao_total_credit),
         credit=idm(domestic_credit)
         )%>%
  ungroup()%>%
  mutate(eez = ifelse(year>=eez_declaration_year,1,0),
         pr1  = case_when(
           gamma == "high"  ~ 1,
           gamma == "med"  ~ 0.5,
           gamma == "low"  ~ 0,
           gamma == "oth" ~ 0.5),
         pr2  = case_when(
           gamma == "high"  ~ 0.75,
           gamma == "med"  ~ 0.5,
           gamma == "low"  ~ 0.25,
           gamma == "oth" ~ 0.5),
         pr = pr1*eez
         )%>%
  as.data.frame()


###################################### Regressions for Table A.11: Fishing fleets, property rights, and credit market development ####################


# main
a1 = feols(ihs(number_agg) ~ credit + pr + pr:credit + gdp + gdp2| country + year + gamma, cluster = ~ country + year, data = x)
summary(a1)
a2 = feols(ihs(tonnage_agg) ~ credit + pr + pr:credit + gdp + gdp2| country + year + gamma, cluster = ~ country + year, data = x)
summary(a2)

etable(a1,a2)


# pr = pr2
b1 = feols(ihs(number_agg) ~ credit + pr + pr:credit + gdp + gdp2| country + year + gamma, cluster = ~ country + year, data = x%>%mutate(pr = pr2*eez))
summary(b1)
b2 = feols(ihs(tonnage_agg) ~ credit + pr + pr:credit + gdp + gdp2| country + year + gamma, cluster = ~ country + year, data = x%>%mutate(pr = pr2*eez))
summary(b2)


# without other
c1 = feols(ihs(number_agg) ~ credit + pr + pr:credit + gdp + gdp2| country + year + gamma, cluster = ~ country + year, data = x%>%filter(gamma!="oth"))
summary(c1)
c2 = feols(ihs(tonnage_agg) ~ credit + pr + pr:credit + gdp + gdp2| country + year + gamma, cluster = ~ country + year, data = x%>%filter(gamma!="oth"))
summary(c2)


# country-year fe 
d1 = feols(ihs(number_agg) ~ credit + pr + pr:credit + gdp + gdp2| country + year + gamma +  country^year  , cluster = ~ country + year, data = x)
summary(d1)
d2 = feols(ihs(tonnage_agg) ~ credit + pr + pr:credit + gdp + gdp2| country + year + gamma  + country^year, cluster = ~ country + year, data = x)
summary(d2)


### print results
etable(a1,a2,b1,b2,c1,c2,d1,d2,
       digits = 3,
       digits.stats = 3,
       drop = c("gdp","gdp2","unemployment_nat","rtfpna","fao_to_credit"),
       dict=c("pr" ="Property rights",
              "credit" = "Credit",
              "gear" = "",
              "country" = "Country FE",
              "year" ="Year FE",
              "credit x pr" = "Credit $\\times$ Property rights",
              "credit:pr" = "Credit $\\times$ Property rights",
              "Year FE (Country FE)" = "Country trend" ,
              "Year FE (gamma)" = "Gear trend",
              "gamma" = "Fleet FE"),
       cluster = ~ country + year,
       replace =T,
       headers = c("Number","Tonnage","Number","Tonnage","Number","Tonnage","Number","Tonnage"),
       depvar = F,
       file = "../Results/boats_final.tex")

#################################### The print the table "Table A.12: Large fishing vessels, property rights, and credit market development"  ###############################
### Etable for large vessels only
# etable(a1,a2,b1,b2,
#        digits = 3,
#        digits.stats = 3,
#        drop = c("gdp","gdp2","unemployment_nat","rtfpna","fao_to_credit"),
#        dict=c("pr" ="Property rights",
#               "credit" = "Credit",
#               "gear" = "",
#               "country" = "Country FE",
#               "year" ="Year FE",
#               "credit x pr" = "Credit $\\times$ Property rights",
#               "credit:pr" = "Credit $\\times$ Property rights",
#               "Year FE (Country FE)" = "Country trend" ,
#               "Year FE (gamma)" = "Gear trend",
#               "gamma" = "Fleet FE"),
#        cluster = ~ country + year,
#        replace =T,
#        headers = c("Number","Tonnage","Number","Tonnage"),
#        depvar = F,
#        file = "../Results/boats_large_final.tex")

